package com.demo.day0308;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 *
 */
@WebServlet(name = "ParameterServlet", urlPatterns = "/parameterServlet")
public class ParameterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //POST请求的中文乱码解决
        //设置请求体的字符集为UTF-8，从而解决post请求的中文乱码问题
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobby");
        //register01.html
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        System.out.println("兴趣爱好:" + Arrays.asList(hobbies));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //获取请求参数
        String username = request.getParameter("username");
        //doGet请求的中文乱码解决
        //1.先以iso8859-1进行编码
        //2.再以utf-8进行编码
        username = new String(username.getBytes("iso-8859-1"), "UTF-8");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobby");
        //register01.html
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        System.out.println("兴趣爱好:" + Arrays.asList(hobbies));
    }
}
